<?php
class Admin_Form_Schedule extends ZendX_JQuery_Form{
	protected $_title;
	protected $_type; // Create or Update
	protected $_submit;

	protected $_fromdate;
	protected $_todate;
	protected $_mon;
	protected $_tue;
	protected $_wed;
	protected $_thu;
	protected $_fri;
	protected $_sat;
	protected $_sun;

	public function __construct($type) {
		parent::__construct();
		$this->_type = $type;
		$this->create($this->_type);
	}

	// Create form base on type.
	protected function create($type) {
		$titleStr  = '';
		$submitStr = '';
		$url = new Zend_View_Helper_Url();
		$action = ' ';
		if ($type == 'create') {
			$action = $url->url(array('module'=>'admin', 'controller'=> 'schedule','action'=>'addschedule'),'default');
			$submitStr = 'Create';
			$titleStr = 'Add schedule';
		} else if ($type == 'update'){
			$action = $url->url(array('module'=>'admin', 'controller'=> 'schedule','action'=>'updateschedule'),'default');
			$submitStr = 'Update';
			$titleStr = 'Update schedule';
		}
		$this->setName($submitStr);
		$this->setAttrib('enctype', 'multipart/form-data');
		$this->setAction($action);

		//table

		$this->_title = new Zend_Form_Decorator_Captcha();
		$this->_title->render($titleStr);

		$this->_fromdate = new ZendX_JQuery_Form_Element_DatePicker('fromdate',
		array('jQueryParams' => array('dateFormat' => 'yy-mm-dd','showWeek'=>true,
			'onSelect' => new Zend_Json_Expr("
                  	function(selectedDate) {
                  		var todate = new Date(selectedDate);
                  		todate.setDate(todate.getDate() + 6);
                  		$('#todate').datepicker('setDate', todate);
                  	}"
			),
			'beforeShowDay' => new Zend_Json_Expr("
					function(date) {
                  		var date = new Date(date);
                  		return [date.getDay() == 1, ''];
                  	}
			")
			))
		);
		$this->_fromdate->setLabel('From date: ')
		->setRequired(true)
		->addValidator('NotEmpty')
		->addValidator(new FromDateVaidate());


		$this->_todate = new ZendX_JQuery_Form_Element_DatePicker('todate',
		array('jQueryParams' => array('dateFormat' => 'yy-mm-dd','showWeek'=>true,
			'onSelect' => new Zend_Json_Expr("
                  	function(selectedDate) {
                  		var fromdate = new Date(selectedDate);
                  		fromdate.setDate(fromdate.getDate() - 6);
                  		$('#fromdate').datepicker('setDate', fromdate);
                  	}"
			),
			'beforeShowDay' => new Zend_Json_Expr("
					function(date) {
                  		var date = new Date(date);
                  		return [date.getDay() == 0, ''];
                  	}
			")
			))
		);
		$this->_todate->setLabel('To date: ')
		->setRequired(true)
		->addValidator('NotEmpty')
		->addValidator(new ToDateVaidate());

		// mon
		$this->_mon = new Zend_Form_Element_Select('mon');
		$this->_mon->setLabel('Mon ');
		// tue
		$this->_tue = new Zend_Form_Element_Select('tue');
		$this->_tue->setLabel('Tue ');
		// mon
		$this->_wed = new Zend_Form_Element_Select('wed');
		$this->_wed->setLabel('Wed ');
		// mon
		$this->_thu = new Zend_Form_Element_Select('thu');
		$this->_thu->setLabel('Thu ');
		// mon
		$this->_fri = new Zend_Form_Element_Select('fri');
		$this->_fri->setLabel('Fri ');
		// mon
		$this->_sat = new Zend_Form_Element_Select('sat');
		$this->_sat->setLabel('Sat ');
		// mon
		$this->_sun = new Zend_Form_Element_Select('sun');
		$this->_sun->setLabel('Sun ');

		$this->_submit = new Zend_Form_Element_Submit('submit');
		$this->_submit->setLabel($submitStr);

		$this->addElements(array($this->_title,
		$this->_fromdate,
		$this->_todate,
		$this->_mon,
		$this->_tue,
		$this->_wed,
		$this->_thu,
		$this->_fri,
		$this->_sat,
		$this->_sun,
		$this->_submit
		));
	}

	public function bindData($menus) {
		$data = array();
		foreach ($menus as $menu) {
			$data[$menu->_mid] = $menu->_menuname;
		}
		$this->_mon->setMultiOptions($data);
		// tue
		$this->_tue->setMultiOptions($data);
		// mon
		$this->_wed->setMultiOptions($data);
		// mon
		$this->_thu->setMultiOptions($data);
		// mon
		$this->_fri->setMultiOptions($data);
		// mon
		$this->_sat->setMultiOptions($data);
		// mon
		$this->_sun->setMultiOptions($data);

	}

	public function populateData(Admin_Model_Schedule $scheduleInfo) {

		if (isset($scheduleInfo->_fromdate)) {
			$this->_fromdate->setValue($scheduleInfo->_fromdate);
		}

		if (isset($scheduleInfo->_todate)) {
			$this->_todate->setValue($scheduleInfo->_todate);
		}

		if (isset($scheduleInfo->_mon)) {
			$this->_mon->setValue($scheduleInfo->_mon);
		}

		if (isset($scheduleInfo->_tue)) {
			$this->_tue->setValue($scheduleInfo->_tue);
		}

		if (isset($scheduleInfo->_wed)) {
			$this->_wed->setValue($scheduleInfo->_wed);
		}

		if (isset($scheduleInfo->_thu)) {
			$this->_thu->setValue($scheduleInfo->_thu);
		}

		if (isset($scheduleInfo->_fri)) {
			$this->_fri->setValue($scheduleInfo->_fri);
		}

		if (isset($scheduleInfo->_sat)) {
			$this->_sat->setValue($scheduleInfo->_sat);
		}

		if (isset($scheduleInfo->_sun)) {
			$this->_sun->setValue($scheduleInfo->_sun);
		}
	}

}

class FromDateVaidate extends Zend_Validate_Abstract {
	const ERROR = 'error';

	protected $_messageTemplates = array(
	self::ERROR => "From date must be monday"
	);
	public function isValid($value)
	{
		if (Zend_Date::isDate($value, DATE_FORMAT)) {
			$date = new Zend_Date();
			$date->setDate($value, DATE_FORMAT);
			if ($date->get(Zend_Date::WEEKDAY_DIGIT) != 1) {
				$this->_error(self::ERROR, $value);
				return false;
			}
		}

		return true;
	}
}

class ToDateVaidate extends Zend_Validate_Abstract {
	const ERROR = 'error';

	protected $_messageTemplates = array(
	self::ERROR => "To date must be sunday"
	);
	public function isValid($value)
	{
		if (Zend_Date::isDate($value, DATE_FORMAT)) {
			$date = new Zend_Date();
			$date->setDate($value, DATE_FORMAT);
			if ($date->get(Zend_Date::WEEKDAY_DIGIT) != 0) {
				$this->_error(self::ERROR, $value);
				return false;
			}
		}
		return true;
	}
}